package com.ts.api.config;

import com.ts.api.interceptor.ChannelHandler;
import com.ts.api.interceptor.ChannelRequestHandler;
import com.ts.api.interceptor.ChannelUserHandler;
import com.ts.api.interceptor.ChannelVisitorHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *  mvc配置
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    //  API渠道
    private static final String DATA_API_PATH = "/data/api/**";
    //  H5渠道采量
    private static final String H5_MARKET_UV_PATH = "/data/api/channel/market/**";
    //  星级回传
    private static final String PLAN_RATING = "/plan/rating/**";
    private static final String DISTRIBUTE_ALL = "/distribute/**";
    //  协议内容
    private static final String PROTOCOL = "/protocol/**";
    //  测试内容
    private static final String TEST_API = "/test/**";

    /** 渠道请求拦截器忽略过滤 */
    private static final String [] CHANNEL_REQUEST_IGNORE_FILTER = {
        "/js/**",
        "/css/**",
        "/images/**"
    };

    /** 渠道拦截器忽略过滤 */
    private static final String [] CHANNEL_IGNORE_FILTER = {
        DATA_API_PATH,
        DISTRIBUTE_ALL,
        PROTOCOL,
        H5_MARKET_UV_PATH,
        PLAN_RATING,
        TEST_API,
        "/common/protocol/**",
        "/js/**",
        "/css/**",
        "/images/**"
    };

    /** 用户拦截器忽略过滤 */
    private static final String [] CHANNEL_USER_IGNORE_FILTER = {
        DISTRIBUTE_ALL,
        DATA_API_PATH,
        PROTOCOL,
        H5_MARKET_UV_PATH,
        PLAN_RATING,
        TEST_API,
        //  公共接口
        "/common/**",
        //  渠道配置接口
        "/channel/config/**",
        //  获取用户token接口
        "/channel/register/getToken",
        //  APP渠道
        "/app/user/login",
        "/app/config/**",
        //  H5渠道
        "/channel/register/list/field",
        "/channel/register/user",
        "/channel/product/feedback",
        "/channel/form/list",
        //  前置贷超匹配
        "/channel/market/pre/list",
        "/channel/market/pre/get/sort",
        //  H5回调页面
        "/h5/callback/**"
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ChannelRequestHandler())
            .addPathPatterns("/**")
            .excludePathPatterns(CHANNEL_REQUEST_IGNORE_FILTER)
        .order(10);

        registry.addInterceptor(new ChannelHandler())
            .addPathPatterns("/**")
            .excludePathPatterns(CHANNEL_IGNORE_FILTER)
        .order(11);

        registry.addInterceptor(new ChannelVisitorHandler())
            .addPathPatterns("/channel/register/list/field")
        .order(12);

        registry.addInterceptor(new ChannelUserHandler())
            .addPathPatterns("/**")
            .excludePathPatterns(CHANNEL_USER_IGNORE_FILTER)
        .order(13);
    }
}
